package com.example.monitoringsystem.utils;

import com.example.monitoringsystem.bean.Health;

public class HealthUtils {
    // 定义温度的界限
    public static final Float MIN_TEMPERATURE = 35.7f;
    public static final Float MAX_TEMPERATURE = 37.3f;
    public static final String LOWER_TEMPERATURE = "温度:%f,偏低";
    public static final String HIGHER_TEMPERATURE = "温度:%f,偏高";

    // 定义心率的界限
    public static final Integer MIN_PULSE = 60;
    public static final Integer MAX_PULSE = 100;
    public static final String LOWER_PULSE = "心率:%d,偏低";
    public static final String HIGHER_PULSE = "心率:%d,偏高";

    // 定义血压的界限
    public static final Integer MIN_BLOOD_PRESSURE = 85;
    public static final Integer MAX_BLOOD_PRESSURE = 139;
    public static final String LOWER_BLOOD_PRESSURE = "血压:%d,偏低";
    public static final String HIGHER_BLOOD_PRESSURE = "血压:%d,偏高";

    // 诊断温度
    public static String diagnosisTemperature(Health health){
        float temperature = health.getTemperature();
        if(temperature<MIN_TEMPERATURE) return String.format(LOWER_TEMPERATURE,temperature);
        else if (temperature>MAX_TEMPERATURE) return String.format(HIGHER_TEMPERATURE,temperature);
        return "";
    }

    // 诊断心率
    public static String diagnosisPLUSE(Health health){
        Integer pulse  = health.getPulse();
        if(pulse<MIN_PULSE) return String.format(LOWER_PULSE,pulse);
        else if (pulse>MAX_PULSE) return String.format(HIGHER_PULSE,pulse);
        return "";
    }

    // 诊断血压
    public static String diagnosisBloodPressure(Health health){
        Integer bloodPressure = health.getBloodPressure();
        if(bloodPressure<MIN_BLOOD_PRESSURE) return String.format(LOWER_BLOOD_PRESSURE,bloodPressure);
        else if (bloodPressure>MAX_BLOOD_PRESSURE) return String.format(HIGHER_BLOOD_PRESSURE,bloodPressure);
        return "";
    }

    private static String appendDiagnostic(String result,String d){
        if(result.length() == 0) return d;
        else if(d.length() == 0) return result;
        else return String.format("%S;%s",result,d);
    }

    // 诊断整体的健康信息
    public static String diagnosisHealth(Health health){
        String result = "";
        result = appendDiagnostic(result,diagnosisTemperature(health));
        result = appendDiagnostic(result,diagnosisPLUSE(health));
        result = appendDiagnostic(result,diagnosisBloodPressure(health));

        return result;
    }
}
